package org.infosys.basic.service.impl;

import org.infosys.basic.mapper.BaseMapper;
import org.infosys.basic.query.BaseQuery;
import org.infosys.basic.service.IBaseService;
import org.infosys.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/6/27 15:16
    * @Parameters: []
    * @Return java.util.List<T>
    */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public PageList<T> loadQuery(BaseQuery baseQuery) {
        // 1.获取到分页总条数
        Integer total = baseMapper.loadQueryCount(baseQuery);

        // 2.如果查询到的总条数为空，那么就没有必要再查询数据
        if (total == 0){
            return new PageList<T>();
        }

        // 3.获取到分页数据
        List<T> rows = baseMapper.loadQuery(baseQuery);

        // 4.返回分页对象
        return new PageList<T>(total, rows);
    }

    @Override
    public void patchDel(Long[] ids) {
        baseMapper.patchDel(ids);
    }


}
